define(["angularAMD"], function (angularAMD) {
    angularAMD.service('MSG02Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            this.init = function(options, feedback){
                function update_slight_state_process(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_MSG.update_slight_state_process',
                        content : content,
                        success : function(data){
                            if(typeof callback === 'function')
                                callback(data);
                        }
                    });
                }

                function openDialog(){
                    $mdDialog.dialog(
                        'module/moduleMSG/program/MSG02/MSG02.tmp.html',
                        function(dialog){
                            return {
                                css_url:'module/moduleMSG/program/MSG02/MSG02.css',
                                data:options.data,
                                mode:'slight_state_'+options.data.slight_state,
                                back: function () {
                                    dialog.hide();
                                },
                                loadReasonList:function(){
                                    $MMWService.showQueryListPNIG({
                                        page_mode: 'slight_reason_info_get',
                                        pageRecord:50,
                                        condition: [
                                            {name:$filter('translate')('common.queryList.reason_c'), column_name:'SLightReasonNo', query_mode:'2', value: dialog.data.slight_reason_no}
                                        ],
                                        header: [
                                            {label:$filter('translate')('common.queryList.reason_c'), code:'reason_no'},
                                            {label:$filter('translate')('common.queryList.reason_n'), code:'reason_name'},
                                            {label:$filter('translate')('common.queryList.description'), code:'description'}
                                        ],
                                        setCondition:function(conditions){
                                            conditions.push({column_name:'SLightTypeNo', query_mode:'0', value:dialog.data.slight_type_no, merge_condition_model:'AND' });
                                            return conditions;
                                        },
                                        selectCode:dialog.data.slight_reason_no,
                                        confirm : function(selectItem){
                                            dialog.data.slight_reason_no = selectItem.reason_no;
                                            dialog.data.slight_reason_name = selectItem.reason_name;
                                            dialog.data.description = selectItem.description;
                                        }
                                    });
                                },
                                confirm:function(){
                                    update_slight_state_process(dialog.data, function(data){
                                        var _data = dialog.data;
                                        _data.slight_state -= 1;
                                        if(_data.slight_state<0)
                                            _data.slight_state = 0;
                                        dialog.hide();
                                        if(typeof(feedback) == 'function'){
                                            feedback(_data);
                                        }
                                    });
                                }
                            }
                        }
                    );
                }

                openDialog();
            }
        }
    ]);
});
